#pragma once

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstring>
#include "mltree/entities/points.hpp"

namespace MetricSpaceBenchmark::MLIndex::MLTree {
    template<typename T>
    void ReadDataFromFileWithNotOId(const std::string &filename, std::vector<Point<T>> &datas) {
        std::ifstream infile(filename);
        if (!infile.is_open()) {
            std::cerr << "无法打开文件 " << filename << std::endl;
        }

        std::string line;
        OID_TYPE Id = 0;
        while (std::getline(infile, line)) {
            std::istringstream iss(line);
            std::string value;
            std::vector<DIS_TYPE> coords;

            while (getline(iss, value, ',')) {
                coords.emplace_back(atof(value.c_str()));
            }

            datas.emplace_back(Id++, T(coords));
        }

        infile.close();
    }

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    template<typename T>
    void ReadDataFromFileWithID(const std::string &filename, std::vector<Point<T>> &datas) {
        std::ifstream infile(filename);
        if (!infile.is_open()) {
            std::cerr << "无法打开文件 " << filename << std::endl;
        }

        std::string line;
        while (std::getline(infile, line)) {
            std::istringstream iss(line);
            std::string value;
            Point<T> point;
            std::vector<DIS_TYPE> coords;

            if (!getline(iss, value, ',')) {
                std::cerr << "读取 id 时出错。" << std::endl;
                continue;
            }
            point.oId = atoi(value.c_str());

            while (getline(iss, value, ',')) {
                coords.emplace_back(atof(value.c_str()));
            }

            if (coords.size() < 2) {
                std::cerr << "坐标数量不正确。" << std::endl;
                continue;
            }

            point.keyObject.key = coords;
            datas.emplace_back(point);
        }

        infile.close();
    }

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    template<typename T>
    void ReadDataFromFile_Query(const std::string &filename, std::vector<Point<T>> &datas) {
        std::ifstream infile(filename);
        if (!infile.is_open()) {
            std::cerr << "无法打开文件 " << filename << std::endl;
        }

        std::string line;
        while (std::getline(infile, line)) {
            std::istringstream iss(line);
            std::string value;
            Point<T> point;
            std::vector<DIS_TYPE> coords;

            while (getline(iss, value, ',')) {
                coords.emplace_back(atof(value.c_str()));
            }

            point.keyObject.key = coords;
            datas.emplace_back(point);
        }

        infile.close();
    }

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    template<typename T>
    void WritePointsToFileWithNotOId(const std::string &filename, const std::vector<Point<T>> &points) {
        std::ofstream outFile(filename);
        if (outFile.is_open()) {
            for (const auto &point: points) {
                point.Print(outFile);
            }
            outFile.close();
        } else {
            std::cerr << "无法打开文件 " << filename << " 进行写入。" << std::endl;
        }
    }

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}

